# -*- coding = utf-8 -*-
# @Time : 2023/7/10 14:41
# @Author : 龙王赘婿_彪少
# @File : mytcp_copy.py
# @Software: PyCharm

# 功能：客户端模板
from socket import *
from threading import Thread


class MYTCP:
    def __init__(self):
        self.ip = "192.168.8.90"
        self.port = 554
        self.client_socket = socket(AF_INET, SOCK_STREAM)
        self.client_socket.connect((self.ip, self.port))
        t1 = Thread(target=self.recv_thread)
        # t2 = Thread(target=self.send_thread)
        t1.start()
        # t2.start()
        t1.join()
        # t2.join()
        self.client_socket.close()

    # 接收线程
    def recv_thread(self):
        while True:
            recv_data = self.client_socket.recv(1024)  # 最大接收1024字节
            self.print_recv_data(recv_data)

    # 打印接收报文
    @staticmethod
    def print_recv_data(raw_data):
        print(f"recv：len = {len(raw_data)} data =", end=" ")
        for i in range(len(raw_data)):
            print(f"{raw_data[i]:02x}", end=" ")
        print()

    # 发送线程
    def send_thread(self):
        while True:
            # 给服务端发消息
            msg = input(">")
            self.client_socket.send(msg.encode("gbk"))
            if msg == "end":
                break


if __name__ == "__main__":
    obj = MYTCP()
